/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron;

import java.awt.image.BufferedImage;
import java.io.Serializable;

/**
 *
 * @author Cenda
 */
public class ImagesPlayer implements Serializable{

    double duration;
    boolean isRepeating;
    transient AnimatedGraphicsLoader sequence;
    double interval;
    double timePassed;
    double globalTickTime;
    int currentImage;
    Runner r;
    public boolean playing;
    int x,y;
    public boolean staticAnimation;

    public ImagesPlayer(AnimatedGraphicsLoader sequence, double duration, int startingPosition, double globalTickTime, boolean isRepeating, Runner r) {
        this.duration = duration;
        this.isRepeating = isRepeating;
        this.sequence = sequence;
        this.globalTickTime = globalTickTime;
        timePassed = 0;
        currentImage = startingPosition;
        interval = duration / sequence.numImages;
        this.r=r;
        playing = false;
        staticAnimation = false;
    }
    /* vola se kazdy herni krok metodou gameUpdate()
     *  inkrementuje cas od posledni zmeny obrazku
     * pokud je cas vetsi nez interval zmeny, preskoci se na dalsi obrazek
     */

    public void updateImage() {
        if(playing){
        timePassed += globalTickTime;
        if (timePassed >= interval) {
            if (currentImage == sequence.numImages) {
            if (isRepeating) {
                resetAnimation();
            } else {
                stopAnimation();
                r.disposeAnimation(this);
                resetAnimation();
            }
            }else{
            currentImage++;
            timePassed = 0;
            }
        }
        
        }
        //System.out.println("currentImage: " + currentImage + ", time passed: " +timePassed);
    }

    public BufferedImage getCurrentImage() {
        return sequence.images[currentImage - 1];
    }

    public BufferedImage getCurrentImage(double angle) {
        return ((RotatingAnimatedGraphicsLoader)sequence).rotatedAnimatedImages[currentImage - 1][(int)Math.floor(Math.toDegrees(angle)/2)];
    }

    public void startAnimation(){
    playing = true;
    }

    public void resetAnimation(){
    currentImage = 1;
    }

    public void stopAnimation(){
    playing = false;
    }

    public void setAnimationLocation(int x, int y){
    this.x = x;
    this.y = y;
    staticAnimation = true;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }



}
